package com.gykj.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class GyReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		try {
			Bundle bundle = intent.getExtras();
			String event_type=EventType.EVENT_UNDEFINED;
			if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
				event_type=EventType.EVENT_REGIST_ID;
			} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
				event_type=EventType.EVENT_MESSAGE_RECEIVE;
			} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
				event_type=EventType.EVENT_NOTIFICATION_RECEIVE;
			} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
				event_type=EventType.EVENT_NOTIFICATION_OPEN;
			} else if (JPushInterface.ACTION_NOTIFICATION_CLICK_ACTION.equals(intent.getAction())) {
				event_type=EventType.EVENT_NOTIFICATION_CLICK;
			} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
			    event_type=EventType.EVENT_CONNECTION_CHANGE;
			}
			processCustomMessage(context,bundle,event_type);
		} catch (Exception e){

		}

	}


	
	//send msg to MainActivity
	private void processCustomMessage(Context context, Bundle  bundle,String action) {
         Toast.makeText(context,"发送信息",Toast.LENGTH_LONG).show();
		if (JPushActivity.isForeground) {
			Intent intent=new Intent(action);
			intent.putExtras(bundle);
			context.sendBroadcast(intent);
		}
	}

}
